#include "timer.h"

#include <chrono>


namespace Database
{

namespace Logging
{

using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
using std::chrono::duration_cast;


Timer::Timer(const string& message, const LogLevel& level)
    : message_(message), logLevel_(level), start_(high_resolution_clock::now()), running_(true)
{ }


Timer::Timer(const char * const message, const LogLevel& level)
    : message_(message ? message : "(null)"), logLevel_(level),
        start_(high_resolution_clock::now()), running_(true)
{ }


Timer::~Timer()
{
    stop();
}


void Timer::stop()
{
    if(running_)
    {
        const auto finish = high_resolution_clock::now();
        Log(logLevel_) << message_ << " took "
            <<  duration_cast<microseconds>(finish - start_).count() << "μs";
        running_ = false;
    }
}


}   // namespace Logging

}   // namespace Database
